//
//  UserWithdrawalController.m
//  CheekiOS
//
//  Created by mac on 2017/6/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserWithdrawalController.h"

#import "AlertViewExtension.h"

#import "UserBindingAccountController.h"



#import "HSLimitText.h"

@interface UserWithdrawalController ()<alertviewExtensionDelegate,UITableViewDelegate, UITableViewDataSource,HSLimitTextDelegate>

@property (nonatomic, strong) AlertViewExtension *alert;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UserpersionlInfoModel *infoModel;
/**
 比例
 */
@property (nonatomic, copy)   NSString  *feeStr;
/**
 魅力值转换的钱
 */
@property (nonatomic, strong) NSString *moneyStr;
/**
 魅力值
 */
@property (nonatomic, strong) NSString *charmStr;
@property (nonatomic, strong) HSLimitText *LimitText;
/**
 转换展示
 */
@property (nonatomic, strong) UILabel *moneyLab;


@end

@implementation UserWithdrawalController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgationBarHiddenNo];
    [self selfTitleFontColor];
    self.title = @"提现";
    
    _moneyStr = @"0";
    _charmStr = @"";
    //绑定后通知刷新数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestDate) name:@"UserBindingAccountNotification" object:nil];
    
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-49) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    
    UIButton *commitBtn = [UIButton new];
    [commitBtn setTitle:@"提现规则" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    commitBtn.frame = CGRectMake(0, 0, 100, 40);
    commitBtn.titleLabel.font = FontP(18);
    [commitBtn addTarget:self action:@selector(clickRitghButton) forControlEvents:UIControlEventTouchUpInside];
    [commitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    commitBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:commitBtn];
    
    
    [self requestDate];
    [self requestFee];
}

-(void)clickRitghButton{

    NSLog(@"提现规则");
}

- (void)requestDate{
    [HcardHttpmanager getPersionalInfosuccess:^(id sucess) {
        
        self.infoModel = [UserpersionlInfoModel mj_objectWithKeyValues:sucess[@"res"]];

        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)requestFee{
    
         [HcardHttpmanager getFeecharm1ToCostSuccess:^(id result) {
             
             _feeStr = result ;
             
         } failure:^(NSError *error) {
             
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        
        static NSString *bindingCellIdent = @"bindingCellIdent";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bindingCellIdent];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:bindingCellIdent];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;//添加箭头
            
        }
        
        cell.backgroundColor = [UIColor clearColor];
        
        
        for (UIView * subView in cell.subviews){
            if ([subView isKindOfClass:[UILabel class]]) {
                [subView removeFromSuperview];
            }
        }
        
        UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, KScreenWidth/2, 50)];
        nameLab.textAlignment = NSTextAlignmentLeft;
        nameLab.textColor = [UIColor blackColor];
        nameLab.font = FontP(15);
        nameLab.text = @"提现账户";
        
        UILabel *withdrawalLab = [[UILabel alloc] initWithFrame:CGRectMake(KScreenWidth-KScreenWidth/2-40, 0, KScreenWidth/2, 50)];
        withdrawalLab.textAlignment = NSTextAlignmentRight;
        withdrawalLab.textColor = [UIColor blackColor];
        withdrawalLab.font = FontP(15);
        
        if(self.infoModel.zfb==nil||self.infoModel.zfbName.length==0){
            withdrawalLab.text = @"未绑定";
            
        }else{
            withdrawalLab.text = @"已绑定";
        }
        
        
        if (_infoModel) {
            [cell addSubview:nameLab];
            [cell addSubview:withdrawalLab];
        }

        
        return cell;
        
    }else if (indexPath.row == 1){
        static NSString *UserWithdrawalCellIdent = @"UserWithdrawalCellIdent";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UserWithdrawalCellIdent];
        if (!cell) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UserWithdrawalCellIdent];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        
        for (UIView * subView in cell.contentView.subviews){
            if ([subView isKindOfClass:[UILabel class]]) {
                [subView removeFromSuperview];
            }
        }
        for (UIView * subView in cell.contentView.subviews){
            if ([subView isKindOfClass:[UIView class]]) {
                [subView removeFromSuperview];
            }
        }

        
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, KScreenWidth-30, 40)];
        
        if (self.infoModel.charm1.length > 0) {
            lab.text = [NSString stringWithFormat:@"提现魅力值（可提现余额%@)",self.infoModel.charm1];

        }else{
            lab.text = @"提现魅力值（可提现余额0)";

        }
        lab.textColor = [UIColor blackColor];
        lab.font = FontP(18);
        [cell.contentView addSubview:lab];
        
        
        _LimitText = [[HSLimitText alloc] initWithFrame:CGRectMake(15, lab.bottom, KScreenWidth-50, 60) type:TextInputTypeTextfield];
        _LimitText.delegate = self;
        _LimitText.placeholder = @"";
        _LimitText.isBecomFirstResponder = NO;
        _LimitText.textField.font = FontP(18);
        _LimitText.maxLength = _infoModel.charm1.length;
        _LimitText.keyType = UIKeyboardTypeNumberPad;
        [cell.contentView addSubview:_LimitText];
        
        UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(15, _LimitText.bottom+5, KScreenWidth-30, 1)];
        linLab.backgroundColor = TheLinColor;
        [cell.contentView addSubview:linLab];
        
        _moneyLab = [[UILabel alloc] initWithFrame:CGRectMake(15, linLab.bottom, KScreenWidth, 40)];
        _moneyLab.textColor = ColorI(0x5978b6);
        _moneyLab.font = FontP(14);
        
        _moneyLab.text = [NSString stringWithFormat:@"到账金额%@元",_moneyStr];
        
        [cell.contentView addSubview:_moneyLab];
        

        return cell;
    }else {
        
    static NSString *WithdrawalBtnCellIdent = @"WithdrawalBtnCellIdent";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:WithdrawalBtnCellIdent];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:WithdrawalBtnCellIdent];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor clearColor];
            UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, KScreenWidth-30, 20)];
            lab.text = @"200魅力值起提，按10的倍数输入 (如210、320)";
            lab.textColor = ColorI(0x2e353f);
            lab.font = FontP(12);
            lab.textAlignment = NSTextAlignmentLeft;
            [cell addSubview:lab];
            
            
            UIButton *btnImg = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnImg addTarget:self action:@selector(withdrawalAction) forControlEvents:UIControlEventTouchUpInside];
            [btnImg setTitle:@"提现" forState:UIControlStateNormal];
            [btnImg setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            btnImg.frame = CGRectMake(24, lab.bottom+100, KScreenWidth-48, 50);
            btnImg.backgroundColor = Colorbackground;
            btnImg.layer.cornerRadius=25;
            
            [cell addSubview:btnImg];

        }
        
      
        
        return cell;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 50;
    }else if (indexPath.row == 1){
        return 160;
    }else{
        return 180;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        UserBindingAccountController *vc = [[UserBindingAccountController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)withdrawalAction{
    
    self.alert =[[AlertViewExtension alloc]initWithFrame:CGRectMake(0, 0,KScreenWidth, KScreenHeight)];
    self.alert.delegate=self;
    [self.alert setbackviewframeWidth:KScreenWidth-50 Andheight:105];
    [self.alert settipeTitleStr:@"确认提现吗" Andfont:14];
    [self.view addSubview:self.alert];
}

//提现弹框代理
-(void)clickBtnSelector:(UIButton *)btn
{
    if (btn.tag == 0) {
        
        if(self.infoModel.zfb==nil||self.infoModel.zfbName.length==0){
            
            UserBindingAccountController *vc = [[UserBindingAccountController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else{
    //判断账户中的余额
            if ([_infoModel.charm1 floatValue] > 200) {
                //判断输入的金额是否大于200
                if ([_charmStr floatValue ]>=200) {
                    
                    
                    NSString *str = [_charmStr substringWithRange:NSMakeRange(_charmStr.length-4, 1)];
                    
                    //判断最后一位是否为0
                    if ([str isEqualToString:@"0"]) {
                        
                        [HcardHttpmanager applyForWithdrawingMoneyWithCost:_moneyStr WithEndCost:_moneyStr WithZfb:_infoModel.zfb Withcharm1:_charmStr  success:^(id result) {
                            
                            [HcardHttpmanager getPersionalInfosuccess:^(id sucess) {
                                
                                self.infoModel = [UserpersionlInfoModel mj_objectWithKeyValues:sucess[@"res"]];
                                
                                [self.tableView reloadData];
                                
                            } failure:^(NSError *error) {
                                
                            }];
                            
                        } failure:^(NSError *error) {
                            
                        }];
                        
                    }else{
                        [SVProgressHUD showWithStatus:@"提现应请输入10的倍数，最后一位应为0"];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [SVProgressHUD dismiss];
                        });
                    }

                }else{
                    [SVProgressHUD showWithStatus:@" 提现魅力值要大于200才可以提现哟"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [SVProgressHUD dismiss];
                    });
                }
                
            }else{
                [SVProgressHUD showWithStatus:@"魅力值要大于200才可以提现哟"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
            }
        }
        [self.alert removeFromSuperview];
    }else{
        [self.alert removeFromSuperview];
    }
}


- (void)limitTextLimitInputOverStop:(HSLimitText *)textLimitInput
{
    
    NSLog(@"超出字数什么也不做");
    
}

- (void)limitTextLimitInput:(HSLimitText *)textLimitInput text:(NSString *)text
{
    _charmStr = [NSString stringWithFormat:@"%.2f",[text floatValue]];
    
    _moneyStr = [NSString stringWithFormat:@"%.2f",[text floatValue]*[_feeStr floatValue]];
    _moneyLab.text = [NSString stringWithFormat:@"到账金额%@元",_moneyStr];

}

@end
